import 'package:json_annotation/json_annotation.dart';

part 'todo.g.dart';

@JsonSerializable()
class Todo {
  final String id;
  String title;
  String? description;
  bool isCompleted;
  int priority; // 1: 高优先级, 2: 中优先级, 3: 低优先级
  final DateTime createdAt;
  DateTime modifiedAt;
  DateTime? dueDate;
  String? category;

  Todo({
    required this.id,
    required this.title,
    this.description,
    this.isCompleted = false,
    this.priority = 2,
    required this.createdAt,
    required this.modifiedAt,
    this.dueDate,
    this.category,
  });

  Todo.create({
    required this.id,
    required this.title,
    this.description,
    this.isCompleted = false,
    this.priority = 2,
    this.dueDate,
    this.category,
  })  : createdAt = DateTime.now(),
        modifiedAt = DateTime.now();

  Todo copyWith({
    String? title,
    String? description,
    bool? isCompleted,
    int? priority,
    DateTime? dueDate,
    String? category,
  }) {
    return Todo(
      id: id,
      title: title ?? this.title,
      description: description ?? this.description,
      isCompleted: isCompleted ?? this.isCompleted,
      priority: priority ?? this.priority,
      createdAt: createdAt,
      modifiedAt: DateTime.now(),
      dueDate: dueDate ?? this.dueDate,
      category: category ?? this.category,
    );
  }

  String get priorityText {
    switch (priority) {
      case 1:
        return '高';
      case 2:
        return '中';
      case 3:
        return '低';
      default:
        return '中';
    }
  }

  factory Todo.fromJson(Map<String, dynamic> json) => _$TodoFromJson(json);
  Map<String, dynamic> toJson() => _$TodoToJson(this);
}